<?php

/**
 * Description of tabs

 * ##CONSTANTE DO SISTEMA QUE PODERA SER USADO
 * #BASEPATH = caminho base da aplicacao public_html/dominio/admin/aplication
 * #URLDOMINIOADMIN = http da aplicacao http://dominio/admin/
 * #
 * @author Adelson
 */
class tabs extends Loadview {

    public $tabs;
    public $xml;
    public $route;

    public function __construct() {
        parent::__construct();
        $this->tabs = loadClass::implementModelClass("modules/tabs/mod_tabs");
        $this->route = loadClass::implementCoreClass("route/routes");
        $this->xml = new Readerxml('module.xml', 'model/modules/tabs');
        $this->tabs->setupModule();
    }

    public function index() {
        //define qual pasta da view esta o arquivo tpl
        //mais info deste metodo http://www.smarty.net/docs/en/api.add.template.dir.tpl
        $this->addTemplateDir(str_replace("\\", "/", BASEPATH . "views/tabs/"), 'tabs');
        if ($this->route->getRoute() === "tabs/addnew" && $_SERVER['REQUEST_METHOD'] === "POST") {
            echo $this->tabs->addShotCodeTabs($_POST);
        } else if ($this->route->getRoute() === "tabs/newpostabs" && $_SERVER['REQUEST_METHOD'] === "POST") {
            echo $this->tabs->addTabs($_POST);
        } else if ($this->route->getRoute() === "tabs/editpost" && $_SERVER['REQUEST_METHOD'] === "POST") {
            echo $this->tabs->updateTabs($_POST,  filter_input(INPUT_GET, 'id'));
        }else if ($this->route->getRoute() === "tabs/delete" && $_SERVER['REQUEST_METHOD'] === "POST") {
            echo $this->tabs->deleteTabs(filter_input(INPUT_POST, 'id'),filter_input(INPUT_POST, 'table'));
        }else {
            $this->implementsVarViews();
            if(!empty(filter_input(INPUT_GET, 'param'))){
                $postTab = $this->tabs->selectTabPost(filter_input(INPUT_GET, 'id'), filter_input(INPUT_GET, 'param'));
                if($postTab){
                    $this->assign('editPostTab', $postTab);
                }else{
                    $this->assign('editPostTab', NULL);
                }
            }else{
                $this->assign('editPostTab', NULL);
            }
            $this->loadView("file:[tabs]tab.tpl", true);
        }
    }

    /* inclui o css e js no DOM do sistema em tempo de execucao */
    public function implementsVarViews() {
        $this->additionalCssFile(array(
            str_replace('root', URLDOMINIOADMIN . 'aplications/model/modules', strval($this->xml->getNodeXml('filenames')->css)),
            str_replace('root/', URLDOMINIOADMIN . 'aplications/model/modules/', strval($this->xml->getNodeXml('filenames')->plugins->easyResponsiveTabs->css))
        ));

        $this->additionalJsFile(array(
            str_replace('root', URLDOMINIOADMIN . 'aplications/model/modules', strval($this->xml->getNodeXml('filenames')->js)),
            str_replace('root/', URLDOMINIOADMIN . 'aplications/model/modules/', strval($this->xml->getNodeXml('filenames')->plugins->easyResponsiveTabs->js))
        ));

        ///XML: variaveis com os nodes XML
        $this->assign("linkRoute", str_replace('root/', URLDOMINIOADMIN, $this->xml->getNodeXml('route', true)));
        //DB
        $this->assign("listTabs", $this->tabs->listTabs()); 
        
        if ($this->route->getRoute() === "tabs/edit" && !empty(filter_input(INPUT_GET, 'id'))) {
            $consulta = $this->tabs->listaTabsPost(filter_input(INPUT_GET, 'id'));
            if(!$consulta){
                $this->assign("listarPostTab",false);
            }else{
                $tabsTitle=null;
                $tabsContent = null;
                foreach ($consulta as $v){
                    $tabsTitle[$v['id']] = $v['nome_tabs'];
                    $tabsContent[] = $v['conteudo_tabs'];
                }
                $this->assign("listarPostTab",true);
                $this->assign('tabsTitle',$tabsTitle);
                $this->assign('tabsContent',$tabsContent);
            }
        }
    }
}
